<?php

/**
 * This is the model class for table "clase".
 *
 * The followings are the available columns in table 'clase':
 * @property integer $id_clase
 * @property integer $id_curso
 * @property string $fecha
 * @property string $tema_materia
 * @property string $observacion
 *
 * The followings are the available model relations:
 * @property Asistencia[] $asistencias
 * @property Curso $idCurso
 * @property Recuperacion[] $recuperacions
 */
class Clase extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Clase the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'clase';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('id_curso, fecha', 'required'),
            array('id_curso', 'numerical', 'integerOnly' => true),
            array('tema_materia, observacion', 'length', 'max' => 255),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id_clase, id_curso, fecha, tema_materia, observacion', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'asistencias' => array(self::HAS_MANY, 'Asistencia', 'id_clase'),
            'idCurso' => array(self::BELONGS_TO, 'Curso', 'id_curso'),
            'recuperacions' => array(self::HAS_MANY, 'Recuperacion', 'id_clase'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id_clase' => 'Clase',
            'id_curso' => 'Curso',
            'fecha' => 'Fecha',
            'tema_materia' => 'Tema Materia',
            'observacion' => 'Observacion',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id_clase', $this->id_clase);
        $criteria->compare('id_curso', $this->id_curso);
        $criteria->compare('fecha', $this->fecha, true);
        $criteria->compare('tema_materia', $this->tema_materia, true);
        $criteria->compare('observacion', $this->observacion, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function dropDownCurso($idProfesor) {

        $soapClient = new SoapClient(Yii::app()->getParams()->webServiceUrl);
        $cursos = $soapClient->getCursoXProfesor($idProfesor);

        $cursoMateria = array(0 => 'Seleccione...');
        foreach ($cursos as $curso) {
            $c = $soapClient->getCurso($curso);
            $materia = $soapClient->getMateria($c['pe_asignatura_id']);
            $carrera = $soapClient->getCarrera($materia['org_carrera_id']);
            $facultad = $soapClient->getFacultad($carrera['org_unid_acad_id']);

            $codMateria = $materia['codigo'];
            $mat = $materia['nombre'];

            $a = ($materia['cursada_anho']);
            $s = ($materia['cursada_periodo_id']);
            if ($s == 1) {
                $semestre = $a * 2 - 1;
            } else {
                $semestre = $a * 2;
            }

            $ca = $carrera['nombre'];
            $fa = $facultad['nombre'];

            $cursoMateria[$curso] = "($codMateria) - $mat - $semestre ° semestre -  $s ° Periodo  - $ca - $fa";
        }
        return $cursoMateria;

        /* CAMBIADO POR SOAP
          $queryString = "select c.id_curso,m.codigo_materia as cod,m.nombre as materia,m.semestre,ca.nombre as carrera,f.nombre as facultad from profesor_x_curso pxc
          join curso c on c.id_curso=pxc.id_curso
          join materia m on m.id_materia=c.id_materia
          join carrera ca on ca.id_carrera=m.id_carrera
          join facultad f on f.id_facultad=ca.id_facultad
          where pxc.id_profesor=$idProfesor";
          $query = Yii::app()->db->createCommand($queryString)->queryAll();
         */
    }

}